﻿/*******************************************************
* Copyright (c) 2018-2088, By XuXiang all rights reserved.
*
* FileName: FightInfo.cpp
* Summary: 战斗信息。
*
* Author: XuXiang
* Date: 2024-05-26 21:55
*******************************************************/

#include "FightInfo.h"
#include "FightDefine.h"
#include <Util/TimeUtil.h>
#include <Util/StringUtil.h>
#include <Frame/MainBase.h>
#include <Frame/ServerBase.h>
#include <Protocol/NetMsgFight.h>
using namespace Competition::Net;

namespace Competition
{
	FightPlayerInfo::FightPlayerInfo(int user_id) : m_UserID(user_id)
	{
	}

	int FightPlayerInfo::PackOperate(Byte* buff, int index, int& op_num)
	{
		if (m_IsMove)
		{
			index = DataUtil::WriteByte(buff, index, (Byte)m_RoleID);
			index = DataUtil::WriteByte(buff, index, (Byte)OperateType::OPT_MOVE);
			index = DataUtil::WriteInt16(buff, index, m_MoveP1);
			index = DataUtil::WriteInt16(buff, index, m_MoveP2);
			op_num++;
		}

		if (m_IsSkillCast)
		{
			index = DataUtil::WriteByte(buff, index, (Byte)m_RoleID);
			index = DataUtil::WriteByte(buff, index, (Byte)OperateType::OPT_CAST_SKILL);
			index = DataUtil::WriteInt32(buff, index, m_SkillID);
			index = DataUtil::WriteInt32(buff, index, m_SkillP1);
			index = DataUtil::WriteInt32(buff, index, m_SkillP2);
			op_num++;
		}

		if (m_IsSkillPress) 
		{
			index = DataUtil::WriteByte(buff, index, (Byte)m_RoleID);
			index = DataUtil::WriteByte(buff, index, (Byte)OperateType::OPT_SKILL_PRESS);
			index = DataUtil::WriteInt32(buff, index, m_SkillID);
			op_num++;
		}

		if (m_IsSkillMove)
		{
			index = DataUtil::WriteByte(buff, index, (Byte)m_RoleID);
			index = DataUtil::WriteByte(buff, index, (Byte)OperateType::OPT_SKILL_MOVE);
			index = DataUtil::WriteInt32(buff, index, m_SkillID);
			index = DataUtil::WriteInt16(buff, index, (Int16)m_SkillP1);
			index = DataUtil::WriteInt16(buff, index, (Int16)m_SkillP2);
			op_num++;
		}

		if (m_IsSkillRelease)
		{
			index = DataUtil::WriteByte(buff, index, (Byte)m_RoleID);
			index = DataUtil::WriteByte(buff, index, (Byte)OperateType::OPT_SKILL_RELEASE);
			index = DataUtil::WriteInt32(buff, index, m_SkillID);
			index = DataUtil::WriteInt16(buff, index, (Int16)m_SkillP1);
			index = DataUtil::WriteInt16(buff, index, (Int16)m_SkillP2);
			op_num++;
		}

		return index;
	}

	FightGroupInfo::FightGroupInfo(int group_id) : m_GroupID(group_id), m_State(0), m_StartTime(0), m_FrameIndex(0)
	{
		m_Recorder = new FightFrameRecord();
	}

	FightGroupInfo::~FightGroupInfo()
	{
		SAFE_DELETE_VECTOR(m_PlayerInfos);
		SAFE_DELETE(m_Recorder);
	}

	FightPlayerInfo* FightGroupInfo::GetPlayer(int user_id)
	{
		for (FightPlayerInfoVector::iterator itr = m_PlayerInfos.begin(); itr != m_PlayerInfos.end(); ++itr)
		{
			if ((*itr)->GetUserID() == user_id)
			{
				return *itr;
			}
		}
		return NULL;
	}

	void FightGroupInfo::UpdateFightFrame(Net::SCFightOperateFrameNotify& ntf)
	{
		Byte* buff = ntf.Bytes;
		int index = 0;
		index = DataUtil::WriteInt32(buff, index, m_FrameIndex++);
		int opnum_write_index = index;
		int opnum = 0;		//操作数量
		index = DataUtil::WriteByte(buff, opnum_write_index, 0);
		for (FightPlayerInfoVector::iterator itr = m_PlayerInfos.begin(); itr != m_PlayerInfos.end(); ++itr)
		{
			index = (*itr)->PackOperate(buff, index, opnum);
			(*itr)->ResetOperate();
		}
		DataUtil::WriteByte(buff, opnum_write_index, (Byte)opnum);		//重新写入操作数量
		ntf.Size = index;
		m_Recorder->AddFrame(ntf);
	}
}

